﻿using System;
using System.Collections;
using ExFlash.Records;

namespace ExFlash.Tags
{
    public class PlaceObject2Tag : Tag
    {
        protected BitArray mFlags;
        protected int mSwfVersion;

        protected const int PlaceFlagHasClipActionsIndex = 0;
        protected const int PlaceFlagHasClipDepthIndex = 1;
        protected const int PlaceFlagHasNameIndex = 2;
        protected const int PlaceFlagHasRatioIndex = 3;
        protected const int PlaceFlagHasColorTransformIndex = 4;
        protected const int PlaceFlagHasMatrixIndex = 5;
        protected const int PlaceFlagHasCharacterIndex = 6;
        protected const int PlaceFlagMoveIndex = 7;

        // Must be the size of the bit array.
        virtual protected int BitArraySize
        {
            get
            {
                return 8;
            }
        }
 
        public Boolean PlaceFlagHasClipActions
        {
            get
            {
                return mFlags[PlaceFlagHasClipActionsIndex];
            }
            protected set
            {
                mFlags[PlaceFlagHasClipActionsIndex] = value;
            }
        }

        public Boolean PlaceFlagHasClipDepth
        {
            get
            {
                return mFlags[PlaceFlagHasClipDepthIndex];
            }
            protected set
            {
                mFlags[PlaceFlagHasClipDepthIndex] = value;
            }
        }

        public Boolean PlaceFlagHasName
        {
            get
            {
                return mFlags[PlaceFlagHasNameIndex];
            }
            protected set
            {
                mFlags[PlaceFlagHasNameIndex] = value;
            }
        }

        public Boolean PlaceFlagHasRatio
        {
            get
            {
                return mFlags[PlaceFlagHasRatioIndex];
            }
            protected set
            {
                mFlags[PlaceFlagHasRatioIndex] = value;
            }
        }

        public Boolean PlaceFlagHasColorTransform
        {
            get
            {
                return mFlags[PlaceFlagHasColorTransformIndex];
            }
            protected set
            {
                mFlags[PlaceFlagHasColorTransformIndex] = value;
            }
        }

        public Boolean PlaceFlagHasMatrix
        {
            get
            {
                return mFlags[PlaceFlagHasMatrixIndex];
            }
            protected set
            {
                mFlags[PlaceFlagHasMatrixIndex] = value;
            }
        }

        public Boolean PlaceFlagHasCharacter
        {
            get
            {
                return mFlags[PlaceFlagHasCharacterIndex];
            }
            protected set
            {
                mFlags[PlaceFlagHasCharacterIndex] = value;
            }
        }

        public Boolean PlaceFlagMove
        {
            get
            {
                return mFlags[PlaceFlagMoveIndex];
            }
            protected set
            {
                mFlags[PlaceFlagMoveIndex] = value;
            }
        }
        
        public UInt16 Depth
        {
            get;
            protected set;
        }

        public UInt16 CharacterId
        {
            get;
            protected set;
        }        

        public MatrixRecord TransformMatrix
        {
            get;
            protected set;
        }

        public CXFormAlphaRecord ColorTransform
        {
            get;
            protected set;
        }

        public UInt16 Ratio
        {
            get;
            protected set;
        }

        public StringRecord Name
        {
            get;
            protected set;
        }

        public UInt16 ClipDepth
        {
            get;
            protected set;
        }

        public ClipActions ClipActions
        {
            get;
            protected set;
        }

        protected PlaceObject2Tag(int len, TagCodes code, int version, Int32 offset)
            : base(code,len, offset)
        {
            mFlags = new BitArray(BitArraySize);
            mSwfVersion = version;
        }

        public PlaceObject2Tag(int len, int version, Int32 offset)
            : this(len,TagCodes.PlaceObject2,version, offset)
        {
        }

        #region ISwfSerializable Members

        public override void SwfDeserialize(BinaryBitReader reader)
        {
            long StartIndex = reader.Position;
            
            if (mSwfVersion >= 5)
            {
                PlaceFlagHasClipActions = reader.ReadBoolean();
            }
            else
            {
                reader.SkipBits(1);
            }

            PlaceFlagHasClipDepth = reader.ReadBoolean();
            PlaceFlagHasName = reader.ReadBoolean();
            PlaceFlagHasRatio = reader.ReadBoolean();
            PlaceFlagHasColorTransform = reader.ReadBoolean();
            PlaceFlagHasMatrix = reader.ReadBoolean();
            PlaceFlagHasCharacter = reader.ReadBoolean();
            PlaceFlagMove = reader.ReadBoolean();
            reader.AlignBits();

            Depth = reader.ReadUInt16();

            if (PlaceFlagHasCharacter)
            {
                CharacterId = reader.ReadUInt16();
            }

            if (PlaceFlagHasMatrix)
            {
                TransformMatrix = new MatrixRecord();
                TransformMatrix.SwfDeserialize(reader);
            }

            if (PlaceFlagHasColorTransform)
            {
                ColorTransform = new CXFormAlphaRecord();
                ColorTransform.SwfDeserialize(reader);
            }

            if (PlaceFlagHasRatio)
            {
                Ratio = reader.ReadUInt16();
            }

            if (PlaceFlagHasName)
            {
                Name = new StringRecord(mSwfVersion);
                Name.SwfDeserialize(reader);
            }

            if (PlaceFlagHasClipDepth)
            {
                ClipDepth = reader.ReadUInt16();
            }

            if (PlaceFlagHasClipActions)
            {
                ClipActions = new ClipActions(mSwfVersion);
                ClipActions.SwfDeserialize(reader);
            }
        }

        public override void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
